<?php

namespace Addons\RedBag;

use Admin\Controller\Addones;
use Common\Controller\Addon;


class RedBagAddon extends Addon
{
    public $info = array(
        'name' => 'RedBag',
        'title' => '红包插件',
        'description' => '可用于发放各种福利',
        'status' => 1,
        'author' => 'Abby_boy',
        'version' => '1.0',
        'has_adminlist' => '1',
    );

    public function install()
    {
        $prefix = C("DB_PREFIX");
        $model = D();
        
        $model->execute("DROP TABLE IF EXISTS `{$prefix}redbag`");
        $model->execute("
CREATE TABLE IF NOT EXISTS `{$prefix}redbag` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `num` int(11) NOT NULL,
  `all_money` double DEFAULT '0',
  `sell_money` double DEFAULT '0',
  `rank` text NOT NULL,
  `content` text NOT NULL,
  `type` varchar(50) NOT NULL,
  `redbag_type` tinyint(4) NOT NULL,
  `create_time` int(11) NOT NULL,
  `status` tinyint(4) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
");

        $model->execute("DROP TABLE IF EXISTS `{$prefix}redbag_list`");
        $model->execute("
CREATE TABLE IF NOT EXISTS `{$prefix}redbag_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `redbagId` int(11) NOT NULL,
  `uid` int(11) NOT NULL,
  `get_bag` double DEFAULT '0',
  `create_time` int(11) NOT NULL,
  `best_luck` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
");

        $model->execute("
INSERT INTO `{$prefix}action` ( `name`, `title`, `remark`, `rule`, `log`, `type`, `status`, `update_time`, `module`) VALUES
( 'send_redbag', '发红包', '发出了一个红包', '', '[user|get_nickname]在[time|time_format]发布了红包：[record|intval]', 1, 1, 1451028409, ''),
( 'get_redbag', '获得红包', '打开并获得了一个红包', '', '[user|get_nickname]在[time|time_format]获得了一个红包：[record|intval]', 1, 1, 1451029151, '');
");
        return true;
    }

    public function uninstall()
    {
        $prefix = C("DB_PREFIX");
        D()->execute("DROP TABLE IF EXISTS `{$prefix}redbag`");
        D()->execute("DROP TABLE IF EXISTS `{$prefix}redbag_list`");
        return true;
    }


//实现钩子
    public function weiboType()
    {
        $this->display(T('Addons://RedBag@RedBag/index'));
    }


    public function fetchRedBag($weibo)
    {
        $weibo_data = unserialize($weibo['data']);
        $redbag = M('Redbag')->where(array('id' => $weibo_data['id']))->find();
        $surplus = $redbag['all_money'] - $redbag['sell_money'];
        $field = D('Ucenter/Score')->getTypeList(array('status' => 1));
        if ($surplus > 0){
            if((time()-$redbag['create_time'])>=86400){
                foreach ($field as &$v) {
                    if ($weibo_data['type'] == $v['id']) {
                        D('Ucenter/Score')->setUserScore($redbag['uid'],$surplus,$v['id'],'inc');
                       // D('Member')->where(array('uid' => $redbag['uid']))->setInc('score' . $v['id'], $surplus);
                        D('Redbag')->where(array('id' => $weibo_data['id']))->setField('status',2);
                        D('Message')->sendMessageWithoutCheckSelf($redbag['uid'], '您的红包并未全部领取，剩余' . $surplus.$v['title'].'已返还。', $v['title'] . '增加了' . $surplus);
                    }
                }
            }
        }
        $user = query_user(array('uid', 'nickname', 'avatar64', 'space_url', 'rank_link', 'title'), $weibo_data['uid']);
        $this->assign('user', $user);
        $weibo_data['weibo_id']=$weibo['id'];
        $this->assign('weibo', $weibo);
        unset($weibo_data['uid']);
        unset($weibo_data['num']);
        unset($weibo_data['all_money']);
        unset($weibo_data['sell_money']);
        unset($weibo_data['rank']);
        unset($weibo_data['type']);
        unset($weibo_data['content']);
        unset($weibo_data['redbag_type']);
        unset($weibo_data['create_time']);
        unset($weibo_data['status']);
       // dump($weibo_data);exit;
        $this->assign('weibo_data', $weibo_data);
        return $this->fetch('redbag');
    }

}